﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RAMED
{
    public partial class RecherchebordereauForm : Form
    {
        public List<Bordereau> RechercheBordoreau = new List<Bordereau>();
        public RecherchebordereauForm()
        {
            InitializeComponent();
            RAMEDEntities1 context = SingletonContext.GetContext();
            comboBoxannexe.DataSource = context.Annexe_administratif;
            comboBoxannexe.DisplayMember = "nom_annexe_administratif";
            comboBoxannexe.SelectedItem = null;

           
           
            
        }

        private void dateTimePickerrechbord_ValueChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            RAMEDEntities1 context = SingletonContext.GetContext();
            List<Bordereau> lesbordereau = new List<Bordereau>();
            lesbordereau = context.Bordereaux.ToList<Bordereau>();
        
            if (checkBoxdateinf.Checked)
            {
                lesbordereau = lesbordereau.Where(c => c.date_bordereau <= dateTimePickerinf.Value).ToList();
            }
            if (checkBoxdatesup.Checked)
            {
                lesbordereau = lesbordereau.Where(c => c.date_bordereau > dateTimePickersup.Value).ToList();
            }



            if (comboBoxannexe.SelectedItem != null)
            {
                Annexe_administratif annexeadministrative = new Annexe_administratif();
                annexeadministrative = (Annexe_administratif)comboBoxannexe.SelectedValue;
                lesbordereau = lesbordereau.Where(p => p.id_annexe_administratif == annexeadministrative.id_annexe_administratif).ToList<Bordereau>();

            }
                        
            if (!txtnumlot.Text.Trim().Equals(""))
            {

                Lot lot = new Lot();
                int num_de_lot = utitlitaire.CasterString(txtnumlot.Text);
                lesbordereau = lesbordereau.Where(p => p.Lot.num_de_lot == num_de_lot).ToList<Bordereau>();

            }

            if (radiobuttonoui.Checked)
            {

                lesbordereau = lesbordereau.Where(p => p.Retrait.Equals(true)).ToList<Bordereau>();

            }
            if (radiobuttonnon.Checked)
            {

                lesbordereau = lesbordereau.Where(p => p.Retrait.Equals(false)).ToList<Bordereau>();

            }

            RechercheBordoreau = lesbordereau;

            mettreAjouterFormGridViewlot();
        }




        private void checkBoxdate_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void dataGridViewrechbor_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void RecherchebordereauForm_Load(object sender, EventArgs e)
        {

        }

        public void mettreAjouterFormGridViewlot()
        {
            dataGridViewrechercheborderaeu.DataSource = null;
            remplirDataGridView();
            dataGridViewrechercheborderaeu.Update();
            dataGridViewrechercheborderaeu.Refresh();
        }

        private void btnmodifier_Click(object sender, EventArgs e)
        {
            if (dataGridViewrechercheborderaeu.CurrentRow == null)
            {
                NotificationTest.NotificationManager nm = new NotificationTest.NotificationManager();
                nm.Show(this, "Veuillez sélectionner un enregistrement pour le modifier", Color.Red, 3000);
            }
            else
            {
                
                modifeirborderaeuForm mbf = new modifeirborderaeuForm();
                RAMEDEntities1 context = SingletonContext.GetContext();
                Bordereau bord = ((Bordereau)dataGridViewrechercheborderaeu.CurrentRow.DataBoundItem);
              


                mbf.Bord = bord;
                mbf.recherbor = this;
                mbf.InitialiserChamps();
                mbf.Show();
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void s_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
        //public RecherchebordereauForm recherbor { get; set; }

        private void declarerRetraitButton_Click(object sender, EventArgs e)
        {
            if (dataGridViewrechercheborderaeu.CurrentRow == null)
            {
                NotificationTest.NotificationManager nm = new NotificationTest.NotificationManager();
                nm.Show(this, "Veuillez sélectionner un enregistrement pour en Declarer un retrait", Color.Red, 3000);
            }
            else
            {


              
                RetraitForm retraitForm = new RetraitForm();
                RAMEDEntities1 context = SingletonContext.GetContext();
                Bordereau bord = new Bordereau();
                int id_borderaeu = ((Bordereau)dataGridViewrechercheborderaeu.CurrentRow.DataBoundItem).id_bordereau;
                bord = context.Bordereaux.Where(d => d.id_bordereau == id_borderaeu).First();

                retraitForm.recherchebordereau = this;
                retraitForm.Bord = bord;
                retraitForm.InitialiserChamps();
                retraitForm.Show();
            }
        }
        private void button2_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }
        public void mettreAjouterFormGridViewbor()
        {
            dataGridViewrechercheborderaeu.DataSource = null;
            remplirDataGridView();
            dataGridViewrechercheborderaeu.Update();
            dataGridViewrechercheborderaeu.Refresh();
        }

        

       

        private void remplirDataGridView()
        {
            dataGridViewrechercheborderaeu.AutoGenerateColumns = false;
            dataGridViewrechercheborderaeu.ColumnCount = 6;

            dataGridViewrechercheborderaeu.Columns[0].HeaderText = "Type";
            dataGridViewrechercheborderaeu.Columns[0].DataPropertyName = "type_bordereau";

            dataGridViewrechercheborderaeu.Columns[1].HeaderText = "Annexe";
            dataGridViewrechercheborderaeu.Columns[1].DataPropertyName = "Annexe_administratif";

            dataGridViewrechercheborderaeu.Columns[2].HeaderText = "Nombre de cartes";
            dataGridViewrechercheborderaeu.Columns[2].DataPropertyName = "Nombre_de_carte";
 
            dataGridViewrechercheborderaeu.Columns[3].HeaderText = "Numero de lot";
            dataGridViewrechercheborderaeu.Columns[3].DataPropertyName = "Lot";

            dataGridViewrechercheborderaeu.Columns[4].HeaderText = "Date";
            dataGridViewrechercheborderaeu.Columns[4].DataPropertyName = "date_bordereau";

            dataGridViewrechercheborderaeu.Columns[5].HeaderText = "Responsable";
            dataGridViewrechercheborderaeu.Columns[5].DataPropertyName = "Responsable";

            dataGridViewrechercheborderaeu.DataSource = RechercheBordoreau;
        }

        private void Button21_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
        }

        private void dateTimePickerinf_ValueChanged(object sender, EventArgs e)
        {

        }


        public object num_de_lot { get; set; }

        private void dataGridViewrechercheborderaeu_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
